<template>
  <div class="forum-post-create-page">
    <el-card class="forum-post-card">
      <div slot="header" class="header">
        <span>📝 发帖</span>
      </div>
      <div class="form-preview-row">
        <!-- 左侧：表单 -->
        <div class="form-area">
          <el-form :model="form" :rules="rules" ref="form" label-width="60px" class="modern-form">
            <el-form-item label="标题" prop="title">
              <el-input
                v-model="form.title"
                maxlength="50"
                show-word-limit
                placeholder="请输入帖子标题"
                size="large"
                clearable
              ></el-input>
            </el-form-item>
            <el-form-item label="内容" prop="content">
              <el-input
                type="textarea"
                v-model="form.content"
                :rows="12"
                placeholder="支持Markdown语法"
                @input="updatePreview"
                size="large"
                class="modern-textarea"
                clearable
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="goBack" size="large" round>取 消</el-button>
              <el-button type="primary" :loading="loading" @click="submitPost" size="large" round>发 帖</el-button>
            </el-form-item>
          </el-form>
        </div>
        <!-- 右侧：预览 -->
        <div class="preview-area">
          <div class="preview-title">内容实时预览</div>
          <div class="preview-content markdown-body" v-html="renderedMarkdown"></div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import { marked } from 'marked';

export default {
  name: "ForumPostCreate",
  data() {
    return {
      form: {
        title: '',
        content: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' },
          { min: 2, max: 50, message: '标题长度2-50字', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' },
          { min: 5, message: '内容至少5个字', trigger: 'blur' }
        ]
      },
      loading: false,
      renderedMarkdown: ''
    }
  },
  methods: {
    goBack() {
      this.$router.push({ path: '/home', query: { tab: 'forum' } });
    },
    updatePreview() {
      this.renderedMarkdown = marked.parse(this.form.content || '');
    },
    async submitPost() {
      this.$refs.form.validate(async (valid) => {
        if (!valid) return;
        this.loading = true;
        try {
          const userInfo = JSON.parse(sessionStorage.getItem('userInfo') || '{}');
          const userId = userInfo.id;
          if (!userId) {
            this.$message.error('请先登录');
            this.loading = false;
            return;
          }
          await this.axios.post('/api/forum/posts', {
            userId: userId,
            title: this.form.title,
            content: this.form.content
          });
          this.$message.success('发帖成功');
          this.$router.push({ path: '/home', query: { tab: 'forum' } });
        } catch (e) {
          this.$message.error('发帖失败，请重试');
        } finally {
          this.loading = false;
        }
      });
    }
  }
}
</script>

<style scoped>
.forum-post-create-page {
  min-height: 100vh;
  background: #f6f8fa;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 0;
}
.forum-post-card {
  width: 1100px;
  max-width: 99vw;
  border-radius: 18px;
  box-shadow: 0 8px 32px rgba(64, 158, 255, 0.10), 0 1.5px 6px rgba(0,0,0,0.04);
  border: none;
  padding-bottom: 0;
}
.header {
  font-size: 28px;
  font-weight: bold;
  color: #409EFF;
  text-align: center;
  letter-spacing: 2px;
  padding: 10px 0;
}
.form-preview-row {
  display: flex;
  gap: 32px;
  padding: 24px 10px 10px 10px;
}
.form-area {
  flex: 1.3;
  min-width: 0;
  max-width: 520px;
}
.preview-area {
  flex: 1.3;
  min-width: 0;
  max-width: 520px;
  background: #f9fafc;
  border-radius: 12px;
  border: 1px solid #e6e6e6;
  padding: 18px 18px 18px 18px;
  box-sizing: border-box;
  max-height: 480px;
  overflow-y: auto;
  box-shadow: 0 2px 8px rgba(64,158,255,0.04);
  margin-top: 8px;
}
.preview-title {
  font-size: 16px;
  color: #409EFF;
  font-weight: 600;
  margin-bottom: 10px;
  letter-spacing: 1px;
}
.preview-content {
  font-size: 16px;
  color: #222;
  min-height: 200px;
  word-break: break-all;
  line-height: 1.7;
}
.modern-form ::v-deep .el-input__inner,
.modern-form ::v-deep .el-textarea__inner {
  border-radius: 8px;
  font-size: 17px;
  background: #f7fafd;
  border: 1.5px solid #e6e6e6;
  transition: border-color 0.2s;
}
.modern-form ::v-deep .el-input__inner:focus,
.modern-form ::v-deep .el-textarea__inner:focus {
  border-color: #409EFF;
  background: #fff;
}
.modern-textarea ::v-deep .el-textarea__inner {
  min-height: 220px;
  max-height: 320px;
  font-size: 16px;
}
.el-button {
  min-width: 100px;
  font-size: 18px;
  margin-right: 18px;
}
.el-button:last-child {
  margin-right: 0;
}
@media (max-width: 900px) {
  .forum-post-card {
    width: 99vw;
    min-width: 0;
    padding: 0;
  }
  .form-preview-row {
    flex-direction: column;
    gap: 18px;
    padding: 10px 2vw;
  }
  .preview-area {
    max-height: 300px;
  }
}
</style> 